const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path');
module.exports = {
    mode: "development",
    devtool: 'source-map',
    entry: {
        index: path.resolve(__dirname, './src/main.js')
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'bundle_[contenthash:8].js',
        clean: true,
    },
    cache: {
        type: 'filesystem'
    },
    devServer: {
        static: {
            directory: path.resolve(__dirname)
        },
        historyApiFallback: true,
        port: 8080,
        hot: true,
        open: true
    },
    module: {
        rules: [
            {
                test: /\.(css|less)$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader']
            },
            {
                test: /\.(js|jsx|ts|tsx)$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: ['@babel/preset-env']
                        }
                    }
                ]
            },
            {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                type: 'asset/resource'
            },
            {
                test: /(\.(eot|ttf|woff|woff2|otf)|font)$/,
                type: 'asset/resource'
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            filename: 'index.html', // 输出的文件名, 默认为 index.html
            template: path.resolve(__dirname, 'index.html')// 需处理的index.html文件
        }),
        new MiniCssExtractPlugin({
            filename: '[name]_[contenthash].css'
        })
    ]
}